package com.dingwen.consumer.controller;

import com.dingwen.commons.entity.Payment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * order controller
 *
 * @author dingwen
 * 2021.07.05 14:20
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    public static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";

    private final RestTemplate restTemplate;

    @Autowired
    public OrderController(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }


    @PostMapping("/consumer/payment")
    public Object save(@RequestBody Payment payment) {
        return restTemplate.postForObject(PAYMENT_URL + "/payment", payment, Object.class);
    }


    @GetMapping("/consumer/{id}")
    public Object find(@PathVariable Long id) {
        return restTemplate.getForObject(PAYMENT_URL + "/payment/" + id, Object.class);
    }


    @GetMapping("/rest/test")
    public ResponseEntity<Payment> restTemplateGetForEntityTest() {
        ResponseEntity<Payment> restTemplateForEntity = restTemplate.getForEntity(PAYMENT_URL + "/payment/rest/test", Payment.class);
        System.out.println(restTemplateForEntity);
        return restTemplateForEntity;
    }

}
